﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;

namespace balace_project_new
{
    public partial class Form2 : Form
    {
        int n ;
        int indexIP ;
        Data dt = new Data();
        Comunication cc = new Comunication();
        UpdateDatagrid ud = new UpdateDatagrid();
        string ViewedBalanceID = "01";
        public Form2()
        {
            InitializeComponent();             
        } 

        private void Form2_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'balance_schemaDataSet.recvdata' table. You can move, or remove it, as needed.
            this.recvdataTableAdapter.Fill(this.balance_schemaDataSet.recvdata);                        
            dt.Get_BalanceProperties();
            dt.Init_StructData();
            ud.Init_dtgDetails(DtgDetails);
            ud.Init_dtgStatic(DtgStatic,dt);
            ud.Init_dtgBalance01(dtgBalance1);
            ud.Init_dtgBalance02(dtgBalance2);
            ud.Init_dtgBalance03(dtgBalance3);
            ud.Init_dtgBalance04(dtgBalance4);
            ud.Init_dtgBalance05(dtgBalance5);
            ud.Init_dtgBalance06(dtgBalance6);

            balance_schemaDataSet.adminDataTable ad = new balance_schemaDataSet.adminDataTable();
            balance_schemaDataSetTableAdapters.adminTableAdapter adAdapter = new balance_schemaDataSetTableAdapters.adminTableAdapter();
            adAdapter.FillBy(ad);
            int.TryParse( ad.Rows[0]["NoOfGateway"].ToString(), out n);
            int.TryParse(ad.Rows[0]["IpIndex"].ToString(), out indexIP);

          //  communication();
           
        }


        //----------------------------- Communication --------------------------------------//
        #region Communication
        private void communication()
        {
            cc.NumberOfGateway = n;
            cc.IpIndex = indexIP;
            cc.structBalancePro = dt.arrBalanceProperties;
           // for (int j = 0; j < cc.NumberOfGateway; j++) cc.structData[j] = dt.arrRevData[j];
            cc.init();
            cc.scan();
           
        }

        private void timer1_communication_Tick(object sender, EventArgs e)
        {
            SaveRecvData();
            Graphics();             
        }
        #endregion

        //=============================== Graphic -- display =================================

        private void tabS_SelectedIndexChanged(object sender, EventArgs e)
        {
            tabBalance.Text = "Cân " + ViewedBalanceID;
            int index = int.Parse(ViewedBalanceID) - 1;
            txtDrugID.Text = dt.arrBalanceProperties[index].DrugID.ToString();
            txtDrugName.Text = dt.arrBalanceProperties[index].DrugName;
            txtManufacturer.Text = dt.arrBalanceProperties[index].DrugManufacturer;
            txtCatergory.Text = dt.arrBalanceProperties[index].DrugCatergory;
            txtWeight.Text = dt.arrBalanceProperties[index].Weight.ToString();
            txtErr.Text = dt.arrBalanceProperties[index].Err.ToString();
            switch (index + 1)
            {
                case 1:
                    dtgBalance1.BringToFront();
                    break;
                case 2:
                    dtgBalance2.BringToFront();
                    break;
                case 3:
                    dtgBalance3.BringToFront();
                    break;
                case 4:
                    dtgBalance4.BringToFront();
                    break;
                case 5:
                    dtgBalance5.BringToFront();
                    break;
                case 6:
                    dtgBalance6.BringToFront();
                    break;
                case 7:
                    dtgBalance7.BringToFront();
                    break;
                case 8:
                    dtgBalance8.BringToFront();
                    break;
                case 9:
                    dtgBalance9.BringToFront();
                    break;
                case 10:
                    dtgBalance10.BringToFront();
                    break;
                case 11:
                    dtgBalance11.BringToFront();
                    break;
                case 12:
                    dtgBalance12.BringToFront();
                    break;
                case 13:
                    dtgBalance13.BringToFront();
                    break;
                case 14:
                    dtgBalance14.BringToFront();
                    break;
                case 15:
                    dtgBalance15.BringToFront();
                    break;
                case 16:
                    dtgBalance16.BringToFront();
                    break;
                case 17:
                    dtgBalance17.BringToFront();
                    break;
                default:
                    dtgBalance1.BringToFront();
                    break;
            }
        }        
        private void RbThongke_CheckedChanged(object sender, EventArgs e)
        {
            if (RbChitiet.Checked)
                DtgDetails.BringToFront();
            else
                DtgStatic.BringToFront();
        }

        private void timer_UpdateDataBase_Tick(object sender, EventArgs e)
        {
            recvdataTableAdapter.Update(balance_schemaDataSet.recvdata);          
        }
        private void Graphics()
        {
            for (int j = 0; j < cc.NumberOfGateway; j++)
            {
                if (cc.DataCome[j] == true)
                {
                    cc.DataCome[j] = false;
                    string s = cc.structData[j].BalanceIP.ToString() + " : " + cc.structData[j].Weight.ToString();
                    MessageBox.Show(s);                    
                    ud.Update_dtgDetails(DtgDetails, cc.structData[j]);
                    ud.Update_dtgStatic(DtgStatic, cc.structData[j]);
                    switch (cc.structData[j].BalanceID)
                    {
                        case "01":
                            ud.Update_dtgBalance(dtgBalance1, cc.structData[j]);
                            break;
                        case "02":
                            ud.Update_dtgBalance(dtgBalance2, cc.structData[j]);
                            break;
                        case "03":
                            ud.Update_dtgBalance(dtgBalance3, cc.structData[j]);
                            break;
                        case "04":
                            ud.Update_dtgBalance(dtgBalance4, cc.structData[j]);
                            break;
                        case "05":
                            ud.Update_dtgBalance(dtgBalance5, cc.structData[j]);
                            break;
                        case "06":
                            ud.Update_dtgBalance(dtgBalance6, cc.structData[j]);
                            break;
                        case "07":
                            ud.Update_dtgBalance(dtgBalance7, cc.structData[j]);
                            break;
                        case "08":
                            ud.Update_dtgBalance(dtgBalance8, cc.structData[j]);
                            break;
                        case "09":
                            ud.Update_dtgBalance(dtgBalance9, cc.structData[j]);
                            break;
                        case "10":
                            ud.Update_dtgBalance(dtgBalance10, cc.structData[j]);
                            break;
                        case "11":
                            ud.Update_dtgBalance(dtgBalance11, cc.structData[j]);
                            break;
                        case "12":
                            ud.Update_dtgBalance(dtgBalance12, cc.structData[j]);
                            break;
                        case "13":
                            ud.Update_dtgBalance(dtgBalance13, cc.structData[j]);
                            break;
                        case "14":
                            ud.Update_dtgBalance(dtgBalance14, cc.structData[j]);
                            break;
                        case "15":
                            ud.Update_dtgBalance(dtgBalance15, cc.structData[j]);
                            break;
                        case "16":
                            ud.Update_dtgBalance(dtgBalance16, cc.structData[j]);
                            break;
                        case "17":
                            ud.Update_dtgBalance(dtgBalance17, cc.structData[j]);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        
        private void SaveRecvData()
        {
            for (int j = 0; j < cc.NumberOfGateway; j++)
            {
                if (cc.DataCome[j] == true)
                {                  
                    Insert2RevDataTable(cc.structData[j]);
                }
            }
            
        }
        private void Insert2RevDataTable(Data.revData Struct_revData)
        {
            balance_schemaDataSet.recvdataDataTable dtb = new balace_project_new.balance_schemaDataSet.recvdataDataTable();
            balance_schemaDataSetTableAdapters.recvdataTableAdapter adt = new balance_schemaDataSetTableAdapters.recvdataTableAdapter();
            adt.Fill(dtb);
            DataRow newRow = dtb.NewRow();
            // newRow["RecvDataID"] = Struct_revData.revDataID;
            newRow["Địa Chỉ"] = Struct_revData.BalanceIP;
            newRow["Cân"] = Struct_revData.BalanceID;
            newRow["Lần Cân"] = Struct_revData.Sequence;
            newRow["Khối Lượng (g)"] = Struct_revData.Weight;
            newRow["Sai Số (g)"] = Struct_revData.WeightErr;
            newRow["Sai Số (%)"] = Struct_revData.Err;
            newRow["Chất Lượng"] = Struct_revData.Quality;
            newRow["Thời gian"] = Struct_revData.Time;
            dtb.Rows.Add(newRow);
            adt.Update(dtb);
        }

        //=============================== Export 2 Excel =================================
        private void label30_Click(object sender, EventArgs e)
        {
           /* Export2Excel exp = new Export2Excel();
            exp.IpIndex = indexIP;
            exp.Information = "sdrfghjkl";
            exp.NumberOfGateway = n;
            exp.Data2Excel();*/
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
           // for (int j = 0; j < n; j++)
           //     if (cc.GatewayStatus[j] == true) cc.client[j].Close();
        }        
    }
}
